package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.SysUser;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @program: itcast_health
 * @create: 2019-10-17 19:06
 * @author: li he
 * @description:
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Reference
    UserService userService;

    @Autowired
    BCryptPasswordEncoder passwordEncoder;


    @RequestMapping("loginSuccess")
    public String loginSuccess(){
        return "redirect:http://localhost:8080/pages/main.html";
    }

    @RequestMapping("loginFail")
    public String loginFail(){
        return "redirect:http://localhost:8080/login.html?error=loginFail";
    }
    @ResponseBody
    @RequestMapping("findUserName")
    public Result findUserName(){

        try {
            SecurityContext context = SecurityContextHolder.getContext();

            Authentication authentication = context.getAuthentication();

            Object principal = authentication.getPrincipal();

            User user = (User) principal;

            String username = user.getUsername();

            return new Result(true, MessageConst.GET_USERNAME_SUCCESS,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.GET_USERNAME_FAIL);
        }


    }
    @ResponseBody
    @RequestMapping("loadUserByUsername")
    public Result loadUserByUsername(String username){
        try {
            SysUser sysUser = userService.loadUserByUsername(username);
            LinkedHashSet<Menu> menus = null;
            for (Role role : sysUser.getRoles()) {
                LinkedHashSet<Menu> ms1 = role.getMenus();
                menus=ms1;
            }
            return new Result(true,MessageConst.ACTION_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.ACTION_FAIL);
        }
    }

    //分页查询用户列表数据
    @ResponseBody
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return userService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    //添加用户
    @ResponseBody
    @RequestMapping("add")
    public Result add(Integer[] roleIds, @RequestBody SysUser sysUser){
        try {
            String saltPassword = passwordEncoder.encode(sysUser.getPassword());
            sysUser.setPassword(saltPassword);
            userService.add(roleIds,sysUser);
            return new Result(true, MessageConst.ADD_User_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_User_FAIL);
        }
    }

    //根据id 查询用户数据
    @ResponseBody
    @RequestMapping("findById")
    public Result findById(Integer id){

        try {
            SysUser sysUser=userService.findById(id);
            return new Result(true,MessageConst.QUERY_USER_SUCCESS,sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_USER_FAIL);
        }

    }

    //根据用户id 查询 用户关联的角色id
    @ResponseBody
    @RequestMapping("findRoleIdAssociationUserIds")
    public Result findRoleIdAssociationUserIds(Integer id){
        try {
            List<Integer> roleIds =userService.findRoleIdAssociationUserIds(id);
            return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ROLE_FAIL);
        }
    }

    //编辑用户信息
    @ResponseBody
    @RequestMapping("edit")
    public Result edit(Integer[] roleIds, @RequestBody SysUser sysUser){
        try {
            userService.edit(roleIds,sysUser);
            return new  Result(true,MessageConst.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new  Result(false,MessageConst.EDIT_USER_FAIL);
        }
    }

    //删除用户信息
    @ResponseBody
    @RequestMapping("delById")
    public Result delById(Integer id){
        try {
            userService.delById(id);
            return new Result(true,MessageConst.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_USER_FAIL);
        }
    }

}